$(window).load(function(){
	$("#lbszipcode").zipplugin();
	$("#expertises").ddlstackable();
	var waitdiv = $("<div></div>");
	waitdiv.addClass("waiting");
	waitdiv.append($('<img src="/images/icons/22x22/view-refresh.png" border="0"style="opacity:1;filter:alpha(opacity=100);top:50%"/>'));
	waitdiv.hide();
	waitdiv.insertBefore($(".searchcontainer"));
	$("#DateDeb").datepicker({ minDate: -20, maxDate: "+1M +10D",appendText: "(dd/mm/yyyy)", dateFormat : "dd/mm/yy",onSelect:function(){$("#DateFin").datepicker("option","minDate",$(this).datepicker( "getDate" ));}});
	$("#DateFin").datepicker({ minDate: -20, maxDate: "+1M +10D",appendText: "(dd/mm/yyyy)", dateFormat : "dd/mm/yy",onSelect:function(){$("#DateDeb").datepicker("option","maxDate",$(this).datepicker( "getDate" ));}});
	$("#develop").click(function(event){
		event.stopImmediatePropagation();
		if($('.searchmoduleadvanced').is(":hidden")) {
			$('.searchmoduleadvanced').slideDown();
			$("#develop").empty();
			$("#develop").append('-');
		}
		else {
			$('.searchmoduleadvanced').slideUp();
			$("#develop").empty();
			$("#develop").append('+');
		}
	});
	//$("#payments").hide();
	//$("#paymentOp").hide();
	$("#payments").change(function(event){
		event.stopImmediatePropagation();
		if($("#paymentOp").val()=="Between") {
			var max = $(this).val();
			$("#paymentroof option").each(function(){
				if(max > 0 && $(this).val()<=max) {$(this).attr("disabled","disabled");}
				else {$(this).removeAttr("disabled");}
			});
		}
		//alert('payment change');
	});
	$("#paymentOp").change(function(event){
		event.stopImmediatePropagation();
		if($(this).val()=="Between") {
			$("#paymentroof").show();
			var max = $("#payments").val();
			$("#paymentroof option").each(function(){
					if(max >0 && $(this).val()<=max) {$(this).attr("disabled","disabled");}
					else {$(this).removeAttr("disabled");}
			});
		}
		else {
			$("#paymentroof").hide();
			$("#paymentroof").val(0);
			$("#payments option").each(function(){$(this).removeAttr("disabled");})
		}
		//alert('paymentOp change');
	});
	$("#paymentroof").hide();
	$("#paymentroof").change(function(event){
		event.stopImmediatePropagation();
		//alert('paymentroof change');
		var max = $(this).val();
		$("#payments option").each(function(){
			if(max>1 && $(this).val()<max) {$(this).removeAttr("disabled");}
			else {$(this).attr("disabled","disabled");}
		});
		
	});
	$('#gosearch').click(function(event){
		event.stopImmediatePropagation();
		var postdata="";
		//get search information;
		if(($("#lbsearch").val()=="" || $("#lbsearch").val()=="titre...")&& $(".searchmoduleadvanced").is(":hidden")==true) {
			return;
		}
		else {
			if(($("#lbsearch").val()!="" && $("#lbsearch").val()!="titre..."))
				postdata +="key="+$("#lbsearch").val(); 
		}
		if($(".searchmoduleadvanced").is(":hidden")==false) {
			if($("#lbszipcode").val()!=""&&$("#lbszipcode").val()!="Zone ...") {
				zip=[];
				$("ul.ui-zipplugin-list > li.ui-autocomplete-item").each(function(){
					zip[zip.length]=$(this).attr('id');
				});
				if(zip.length>0) {
				postdata +="&zip=";
				for(var i =0;i<zip.length;i++)
					if(zip[i]!='')
						postdata +=zip[i]+",";
				}
			}
			if($("#DateDeb").val()!="") {
				dd = $("#DateDeb").datepicker('getDate');
				postdata+="&datedebut="+dd.toISOString().replace('T',' ').replace('Z','');
			}
			if($("#DateFin").val()!="") {
				df = $("#DateFin").datepicker('getDate');
				postdata+="&datefin="+df.toISOString().replace('T',' ').replace('Z','');
				
			}
			if($("#expertises").val()!="" && $("#expertises").val()!="NULL"){
				//alert($("#expertises").val());
				postdata+="&exp="+$("#expertises").val();
			}
			if($("#payments").val()>0) {
				postdata+="&payment="+$("#payments").val();
			}
			if($("#payments").val()>0) {
				postdata+="&paymentop="+$("#paymentOp").val();
			}
			if($("#paymentOp").val()=="Between") {
				if($("#paymentroof").val()==0) {
					var rf = 999;
				}
				else {
					var rf = $("#paymentroof").val();
				}
				postdata+="&paymenroof="+rf;
			}
		}
		if(postdata){
			var containerpos = $(".searchcontainer").position();
			waitdiv.css({"height":$(".searchcontainer").innerHeight(),"width":$(".searchcontainer").innerWidth(),"top":containerpos.top,"left":containerpos.left})
			waitdiv.show();
		var url = 'http://bestcoming.localhost/default/training/search';
		$.ajax({
			url:url,
			data:postdata,
			type:"POST",
			success:function(data){
				outdecode = jQuery.parseJSON(data);
				$(".searchcontainer").children().remove();
				//items = jQuery.parseJSON(outdecode.data);
				items = outdecode.data;
				nbpage = outdecode.nbpage;
				for(var i=0;i<items.length;i++) {
					var tr = $("<tr></tr>");
					tr.append($("<td></td>").append(items[i].title));
					tr.append($("<td></td>").append(items[i].detail));
					tr.append($("<td></td>").append(items[i].payment));
					var td = $("<td></td>");
					
					td.append($('<a href="/default/training/show/id/'+items[i].id+'">detail</a>'));
					if(items[i].editabled)
						td.append($('<a href="/default/training/edit/id/'+items[i].id+'">edit</a>'));
				
					tr.append(td);
					$(".searchcontainer").append(tr);
				}
				waitdiv.hide();
				if(nbpage>"1") {
					$(".searchcontainer + tfoot").children().remove();
					tr = $("<tr></tr>");
					td = $("<td></td>");
					td.attr("colspan",2);
					td.css("text-align","center");
					td.append("Pages : ");
					for(var i =0 ;i<nbpage;i++) {
						a = $('<a href="#"></a>');
						a.addClass('paging');
						if(i=0)
							a.addClass('pageselected');
						a.append((i+1));
						td.append(a);
					}
					tr.append(td);
					$(".searchcontainer + tfoot").append(tr);
					$(".paging").click(pageclick);
				}
				else{$(".searchcontainer + tfoot").children().remove();}
			}
		});}
	});
	
	$(".paging").click(pageclick);	
});
	
	var pageclick = function(event){
		event.stopImmediatePropagation();
		var url = "http://bestcoming.localhost/default/training/nextpage/page/"+$(this).text();
		page = $(".pageselected").text();
		if(page!=$(this).text()) {
			$(".pageselected").removeClass("pageselected");
			$(this).addClass('pageselected');
			$.ajax({
				url:url,
				success:function(data){
					outdecode = jQuery.parseJSON(data);
					$(".searchcontainer").children().remove();
					for(var i=0;i<outdecode.length;i++) {
						var tr = $("<tr></tr>");
						tr.append($("<td></td>").append(outdecode[i].User));
						tr.append($("<td></td>").append(outdecode[i].title));
						tr.append($("<td></td>").append(outdecode[i].payment));
						tr.append($("<td></td>").append(outdecode[i].Created));
						var td = $("<td></td>");
						
						td.append($('<a href="/default/training/show/id/'+outdecode[i].id+'">detail</a>'));
						if(outdecode[i].editabled)
							td.append($('<a href="/default/training/edit/id/'+outdecode[i].id+'">detail</a>'));
					
						tr.append(td);
						$(".searchcontainer").append(tr);
					}
				}
			});
		}
	}